﻿using System;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Collections.Generic;
using System.Windows.Media.Imaging;

using Cratos.Desktop;

namespace Cratos.Generator
{
	/// <summary>
	/// Open Project Window class
	/// </summary>
	public partial class OpenProjectView : Window
	{
		#region Class Constant declarations
		#endregion

		#region Class Event declarations
		private delegate void RefreshData(object sender, ExecutedRoutedEventArgs e);
		#endregion

		#region Custom Routed Commands
		/// <summary>
		/// Open custom command
		/// </summary>
		public static RoutedCommand OpenProjectCommand = new RoutedCommand();

		/// <summary>
		/// Cancel custom command
		/// </summary>
		public static RoutedCommand CancelCommand = new RoutedCommand();
		#endregion

		#region Class Variable declarations
		private Timer m_ViewTimer;
		#endregion

		#region Class Constructors & Destructors
		/// <summary>
		/// Class Constructor
		/// </summary>
		public OpenProjectView()
		{
			// Initailize the relevant components
			InitializeComponent();

			// Create, setup and activate the Server Timer
			this.m_ViewTimer					= new Timer(200);
			this.m_ViewTimer.Elapsed += new ElapsedEventHandler(this.OnViewTimerElapsed);
			this.m_ViewTimer.Enabled	= true;
		}
		#endregion

		#region Class Properties
		/// <summary>
		/// Presenter
		/// </summary>
		public OpenProjectPresenter Presenter
		{
			get { return (this.DataContext as OpenProjectPresenter); }
			set
			{
				// Save the relevant data
				this.DataContext = value;

				// Subscribe to the relevant events
				this.Presenter.ProgressStart	+= new MVPProgressEvent(this.OnProgressStart);
				this.Presenter.ProgressEnd		+= new MVPProgressEvent(this.OnProgressEnd);
				this.Presenter.ProgressUpdate += new MVPProgressUpdateEvent(this.OnProgressUpdate);
			}
		}
		#endregion

		#region Class Methods
		#endregion

		#region Class Window Event handlers
		private void OnWindowLoaded(object sender, RoutedEventArgs e)
		{
			// Declare the required variables

			// Return to the calling method
			return;
		}

		private void OnViewTimerElapsed(object sender, ElapsedEventArgs e)
		{
			// Declare the required variables

			// Disable the Timer
			this.m_ViewTimer.Enabled = false;

			// Update the Status bar
			this.Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal,
																	new RefreshData(this.OnRefreshData), null, null);

			// Return to calling method
			return;
		}

		#region Control Commands
		private void OnOpenProjectCommand(object sender, ExecutedRoutedEventArgs e)
		{
			// Declare the required variables

			// Open the relevant Project
			this.Presenter.OpenProject();

			// Return to calling method
			return;
		}

		private void OnOpenProjectCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			// Declare the required variables

			// Set the relevant option
			if ((this.DataContext != null) && (this.m_ListProjects != null))
			{
				e.CanExecute = (this.m_ListProjects.SelectedItem != null);
			}

			// Return to calling method
			return;
		}

		private void OnCancelCommand(object sender, ExecutedRoutedEventArgs e)
		{
			// Declare the required variables

			// Close the window
			this.Close();

			// Return to calling method
			return;
		}

		private void OnCancelCommandCanExecute(object sender, CanExecuteRoutedEventArgs e)
		{
			// Declare the required variables

			// Set the relevant option
			e.CanExecute = true;

			// Return to calling method
			return;
		}
		#endregion
		#endregion

		#region Class Support Methods
		private void OnProgressStart()
		{
			// Declare the required variables

			// Display the Progress Message
			this.m_BorderProgress.Visibility = Visibility.Visible;
			this.m_LblProgress.Visibility    = Visibility.Visible;
			CratosWPFHelper.DoEvents();

			// Return to calling method
			return;
		}

		private void OnProgressEnd()
		{
			// Declare the required variables

			// Display the Progress Message
			this.m_BorderProgress.Visibility = Visibility.Hidden;
			this.m_LblProgress.Visibility    = Visibility.Hidden;
			CratosWPFHelper.DoEvents();

			// Return to calling method
			return;
		}

		private void OnProgressUpdate(string progressMessage)
		{
			// Declare the required variables

			// Display the Progress Message
			this.m_LblProgress.Text = progressMessage;
			CratosWPFHelper.DoEvents();

			// Return to calling method
			return;
		}

		private void OnRefreshData(object sender, ExecutedRoutedEventArgs e)
		{
			// Declare the required variables

			// Load the relevant data
			if (this.DataContext != null) { this.Presenter.LoadAllProjects(); }

			// Return to the calling method
			return;
		}
		#endregion
	}
}
